package com.pks.datacenter.controller.theme;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pks.datacenter.entity.theme.ThemeDbView;
import com.pks.datacenter.service.theme.ThemeDbViewService;
import com.pks.resultful.common.SRemoteResult;
import com.pks.util.SUtilValidate;
import com.pks.util.UtilConvert;
import com.pks.web.springmvc.controller.BasicController;


@RestController
@RequestMapping(value = "/themeDbView")
@Scope("prototype")
@Component("themeDbViewController")
@Api(tags = "主题表视图")
public class ThemeDbViewController extends BasicController {

	@Resource
	private ThemeDbViewService themeDbViewService;
	
	@ApiOperation(value = "列表查询")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/list" , method = RequestMethod.POST)
    public SRemoteResult<List<ThemeDbView>> list(HttpServletRequest request, Long dbInfoId, String nameLike, Integer offset,Integer pagesize) {
        Map<String, Object> qbuilder = new HashMap<String, Object>();
        if (dbInfoId != null) {
			qbuilder.put("dbInfoId", dbInfoId);
		}
        if (StringUtils.isNotBlank(nameLike)) {
			qbuilder.put("nameLike", nameLike.trim());
		}
        List<ThemeDbView> list = new ArrayList<ThemeDbView>();
        int total = 0;
        total = themeDbViewService.getTotalRows(qbuilder);
        if (total > 0) {
            list = themeDbViewService.findByPage(null, qbuilder);
        }
        return SRemoteResult.initPageList(list, total);
    }

	@ApiOperation(value = "新增")
    @RequestMapping(value = "/add" , method = RequestMethod.POST)
    public SRemoteResult<String> add(HttpServletRequest request, ThemeDbView obj) {
        if (obj == null) {
            return SRemoteResult.initErrorParamMissing(null);
        }
        SRemoteResult<String> validate = SUtilValidate.validate(obj);
        if (!validate.success()) {
            return validate;
        }
        try {
			if (StringUtils.isNotBlank(obj.getQueryJson())) {
				JSONObject json = JSON.parseObject(obj.getQueryJson());
				if (json.entrySet().size() == 0) {
					return SRemoteResult.initError("查询条件为空");
				}
			}
		} catch (Exception e) {
			return SRemoteResult.initError("查询参数格式错误");
		}
        obj.insertBefore();
        themeDbViewService.insert(obj);
        return SRemoteResult.initSuccess(null);
    }
	
	@ApiOperation(value = "修改")
	@RequestMapping(value = "/modify" , method = RequestMethod.POST)
	public SRemoteResult<String> modify(HttpServletRequest request, ThemeDbView obj) {
		if (obj == null || obj.getId() == null) {
			return SRemoteResult.initErrorParamMissing(null);
		}
		SRemoteResult<String> validate = SUtilValidate.validate(obj);
        if (!validate.success()) {
            return validate;
        }
		ThemeDbView update = themeDbViewService.findById(obj.getId());
		if (update == null) {
			return SRemoteResult.initErrorDataNone();
		}
		themeDbViewService.update(obj);
		return SRemoteResult.initSuccess(null);
	}
	
	@ApiOperation(value = "删除记录")
    @RequestMapping(value = "/delete" , method = RequestMethod.POST)
    public SRemoteResult<String> delete(HttpServletRequest request, String ids) {
        if (StringUtils.isBlank(ids)) {
            return SRemoteResult.initErrorParamBlank(null);
        }
        if (StringUtils.isBlank(ids) || !ids.matches("^[0-9]+(,[0-9]+)*$")) {
            return SRemoteResult.initErrorParamInvalid(null);
        }
        List<Long> idList = UtilConvert.string2LongList(ids);
        if (idList.size() == 1) {
            themeDbViewService.delete(idList.get(0));
        } else {
            themeDbViewService.deleteByIds(idList);
        }
        return SRemoteResult.initSuccess(null);
    }

	@ApiOperation(value = "获取详情")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id",value = "id", dataType = "long" , paramType = "query" )
	})
	@RequestMapping(value = "/get" , method = {RequestMethod.GET,RequestMethod.POST})
	public SRemoteResult<ThemeDbView> findInfo(HttpServletRequest request, Long id) {
		if (id == null) {
			return SRemoteResult.initErrorParamMissing(null);
		}
		ThemeDbView obj = themeDbViewService.findById(id);
		if (obj == null) {
			return SRemoteResult.initErrorDataNone();
		}
		return SRemoteResult.initSuccess(obj);
	}

    @ApiOperation(value = "数据查询（不分页）")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/data" , method = RequestMethod.POST)
    public SRemoteResult<List<ThemeDbView>> data(HttpServletRequest request ) {
        Map<String, Object> qbuilder = new HashMap<String, Object>();
        List<ThemeDbView> list = new ArrayList<ThemeDbView>();
        list = themeDbViewService.find(null, qbuilder, null);
        return SRemoteResult.initPageList(list, list.size());
    }


}
